import 'package:flutter/material.dart';
import 'package:getx_learn/app/modules/weichat_post/util/du_picker.dart';
import 'package:getx_learn/app/modules/weichat_post/widgets/divider.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

enum PickType { camera, asset }

/// 微信底部弹出
class DuBottomSheet {
  // 已选中的图片
  final List<AssetEntity>? selectedAssets;

  DuBottomSheet(this.selectedAssets);

  /// 选择拍摄、相机资源
  Future<T?> wxPicker<T>(BuildContext context) {
    return Dupicker.showModalSheet<T>(context,
        child: _buildAssetCamera(context));
  }

  /// 相册、相机
  Column _buildAssetCamera(BuildContext context) {
    return Column(
      // 这里可以设置高度，只包裹内容，类似 wrap_content
      mainAxisSize: MainAxisSize.min,
      children: [
        // 拍摄
        _buildBtn(Text('拍摄'), onTap: () {
          Dupicker.showModalSheet(
            context,
            child: _buildMediaImageVideo(context, pickType: PickType.camera),
          );
        }),

        DividerWidget(
          height: 1,
        ),

        // 相册
        _buildBtn(Text('相册'), onTap: () async {
          // List<AssetEntity>? result;
          // result = await Dupicker.assets(
          //   context: context,
          //   requestType: RequestType.image,
          //   selectedAssets: selectedAssets,
          // );
          //
          // if (result == null) {
          //   return;
          // }
          //
          // Navigator.pop(context, result);

          Dupicker.showModalSheet(
            context,
            child: _buildMediaImageVideo(context, pickType: PickType.asset),
          );
        }),

        DividerWidget(
          height: 6,
        ),
        // 取消
        _buildBtn(
          Text('取消'),
          onTap: () {
            Navigator.pop(context);
          },
        ),
      ],
    );
  }

  /// 图片、视频
  Column _buildMediaImageVideo(BuildContext context,
      {PickType pickType = PickType.camera}) {
    return Column(
      // 这里可以设置高度，只包裹内容，类似 wrap_content
      mainAxisSize: MainAxisSize.min,
      children: [
        // 拍摄
        _buildBtn(Text('图片'), onTap: () async {
          List<AssetEntity>? result;
          if (pickType == PickType.asset) {
            result = await Dupicker.assets(
              context: context,
              requestType: RequestType.image,
              selectedAssets: selectedAssets,
            );
          } else if (pickType == PickType.camera) {
            final asset = await Dupicker.takePhoto(context);
            if (asset == null) {
              return;
            }
            if (selectedAssets == null) {
              result = [asset];
            } else {
              /// ... 表示解构，就是让数组平铺
              result = [...selectedAssets!, asset];
            }
          }
          _popRoute(context, result: result);
        }),

        DividerWidget(
          height: 1,
        ),

        // 相册
        _buildBtn(Text('视频'), onTap: () async {
          if (pickType == PickType.asset) {
            List<AssetEntity>? result;
            final selectedList = <AssetEntity>[];
            for(var asset in selectedAssets!){
              if(asset.type == AssetType.video){
                selectedList.add(asset);
              }
            }
            result = await Dupicker.assets(
              context: context,
              requestType: RequestType.video,
              selectedAssets: selectedList,
              maxAssets: 1
            );
            if(result == null){
              return;
            }
            _popRoute(context, result: result);
            return;
          }

          final result = await Dupicker.taskVideo(context);
          if (result == null) {
            return;
          }
          _popRoute(context, result: [result]);
        }),

        DividerWidget(
          height: 6,
        ),
        // 取消
        _buildBtn(Text('取消'), onTap: () {
          // 关闭两层弹出框，方式1，调用多次 Navigator.pop
          // Navigator.pop(context);
          // Navigator.pop(context);
          _popRoute(context);

          // // 方式2 (不推荐)，推荐使用方式1
          // Navigator.popUntil(context, (router)=>router.isFirst);
        }),
      ],
    );
  }

  void _popRoute(BuildContext context, {result}) {
    Navigator.pop(context);
    Navigator.pop(context, result);
  }

  InkWell _buildBtn(Widget child, {void Function()? onTap = null}) {
    return InkWell(
      onTap: onTap,
      child: Container(
        alignment: Alignment.center,
        height: 40,
        child: child,
      ),
    );
  }
}
